<?php

	
	require_once('logic\connection.php');
	require_once('logic\chksession.php');	
	if(isset($_POST['isbn']))
	{
	$isbn=$_POST['isbn'];
	$user_id=$_SESSION['user_id'];
	
	$res=$con->prepare("select credit_amount from user_credit where user_id=?");
	$res->bind_param("i",$user_id);
	$res->execute();
	$res->bind_result($amt);
	$res->fetch();
	$con=getConnection();
	$res=$con->prepare("select price from book_info where isbn=?");
	$res->bind_param("s",$isbn);
	$res->execute();
	$res->bind_result($bamt);
	$res->fetch();
	
	$con=getConnection();
	$res=$con->prepare("select user_id from user_books where user_id=? and isbn=?");
	$res->bind_param("is",$user_id,$isbn);
	$res->execute();
	$res->bind_result($uid);
	$res->fetch();
	//echo $uid;
	$flag=0;
	//echo $uid;
	//echo $flag;
	if($amt<$bamt || $uid!="")
	{
		if($uid!="")
			echo "Book already downloaded";
		else
			echo "Not enough amount";
		$flag=1;
	}
	
	if(isset($_REQUEST['download']))
	{
		$amt=$amt-$bamt;
		//echo $amt;
		$con=getConnection();
		$res=$con->prepare("UPDATE user_credit SET credit_amount = ? WHERE user_id=? ");
		$res->bind_param('ii',$amt,$user_id);
		$res->execute();
		$con=getConnection();
		$res=$con->prepare("insert into user_books(user_id,isbn) values(?,?)");		
		$res->bind_param("is",$user_id,$isbn);
		$res->execute();
		$con=getConnection();
		$res=$con->prepare("select file_path from book_info where isbn=?");
		$res->bind_param('s',$isbn);
		//echo $isbn."   ----    ";
		$res->execute();
		$res->bind_result($path);
		$res->fetch();
		//echo $path."   ----    ";
		$flag=2;
	}
	}
?>
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

	<title></title>
	<meta name="description" content="">
	<meta name="author" content="">

	<meta name="viewport" content="width=device-width">

	<link rel="stylesheet" href="css/bootstrap.min.css">
	<style>
	body {
	  padding-top: 60px;
	  padding-bottom: 40px;
	}
	</style>
	<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
	<link rel="stylesheet" href="css/style.css">
	
	<script src="js/libs/modernizr-2.5.3-respond-1.1.0.min.js"></script>
</head>
<body>
<!--[if lt IE 7]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->

    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="index.php">E-library</a>
          <div class="nav-collapse">
            <ul class="nav">
              <li class="active"><a href="index.php">Home</a></li>
              <?php
               if(isset($_SESSION['user_id'])){ ?>
				  
				 <li><a href="addcredit.php">Add Credit</a></li>
				  <li><a href="logic/logout.php">Log Out</a></li>
                  <li><a href="my-downloads.php">My Downloads</a></li>
                  
                  <?php
				   } 
				  else {
				  ?>
				  <li><a href="register.php">Register</a></li>
				  <li><a href="login.php">Log In</a></li>
                  
                  <?php }?>
			 
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">
<?php
	if(isset($_POST['isbn']))
	{
		if($flag==0)
		{
?>
<div class="hero-unit">
<form method="post">
	<p>
        <input type="hidden" value="<?php echo $isbn; ?>" name="isbn">
        <p>Are you sure to buy this book?
        <input type="submit" class="btn btn-primary" value="Yes" name="download" /></p>
    </p>
</form>
</div>
<?php
	$flag=3;
		}
	//echo $flag;
	}
?>
<?php
	if(isset($_POST['isbn']))
	{
		if($flag==2)
		{
?>
	<div class="hero-unit">
	<a href="books/<?php echo $path; ?>">click to Download your book.</a>
    </div>
<?php
		}
	}
?>
</div>
</body>
</html>